package com.chendx.store.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chendx.model.common.dtos.PageResponseResult;
import com.chendx.model.common.dtos.ResponseResult;
import com.chendx.model.common.enums.AppHttpCodeEnum;
import com.chendx.model.goods.dto.AlbumDto;
import com.chendx.model.goods.pojo.Album;
import com.chendx.store.goods.mapper.TbAlbumMapper;
import com.chendx.store.goods.service.TbAlbumService;
import org.springframework.stereotype.Service;

/**
 * Created by chendx on 2022/4/15.
 */
@Service
public class TbAlbulmServiceImpl extends ServiceImpl<TbAlbumMapper,Album> implements TbAlbumService {


    @Override
    public ResponseResult findPage(AlbumDto dto) {
        //1.检查参数
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数检查
        dto.checkParam();
        LambdaQueryWrapper<Album> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotEmpty(dto.getTitle())){
            queryWrapper.like(Album::getTitle,dto.getTitle());
        }
        Page ipage = new Page<>(dto.getPage(), dto.getSize());
        IPage<Album> result = page(ipage, queryWrapper);
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(), (int) result.getTotal());
        responseResult.setData(result.getRecords());
        return responseResult;
    }
}
